package com.zykj.thread.concurrent.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class SynchronizedDemo {

    public static void main(String[] args) {
        final OutPut outPut = new OutPut();
        new Thread(new Runnable() {
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    outPut.output("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
                }
            }
        }).start();

        new Thread(new Runnable() {
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
//                    outPut.output("cccccccccccccccccccccccccccccccccccccccccccccc");
                    outPut.output("cccccccccccccccccccccccccccccccccccccccccccccc");
                }
            }
        }).start();


    }

    static class OutPut {
        Lock lock = new ReentrantLock();
        public void output(String str) {
            lock.lock();
            try {
                for (int i = 0; i < str.length() ; i++) {
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.print(str.charAt(i));
                }
                System.out.println();
            }finally {
                lock.unlock();
            }

        }
    }
}